package com.hfut.jpoa.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.exception.DaoException;
import com.hfut.jpoa.dao.ProviderDAO;
import com.hfut.jpoa.dto.ProviderDTO;
import com.hfut.jpoa.model.Provider;
import com.ibatis.sqlmap.client.SqlMapClient;

public class ProviderDAOImpl implements ProviderDAO {
	
	private static final Logger LOGGER = LoggerFactory
	        .getLogger(SysparaDAOImpl.class);
	
	private SqlMapClient sqlMapClient;


    public void insert(Provider record) throws DaoException {
        try {
			sqlMapClient.insert("tb_jpoa_provider.ibatorgenerated_insert", record);
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }

    public void insertSelective(Provider record) throws DaoException {
        //sqlMapClient.insert("tb_jpoa_provider.ibatorgenerated_insertSelective", record);
    }

    public Provider selectByPrimaryKey(String id) throws DaoException {
        Provider key = new Provider();
        key.setId(id);
        
        Provider record = null;
		try {
			record = (Provider) sqlMapClient.queryForObject("tb_jpoa_provider.ibatorgenerated_selectByPrimaryKey", key);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
        return record;
    }

    public int updateByPrimaryKeySelective(Provider record) throws DaoException {
        int rows = 0;
		try {
			rows = sqlMapClient.update("tb_jpoa_provider.ibatorgenerated_updateByPrimaryKeySelective", record);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return rows;
    }

    public int updateByPrimaryKey(Provider record) throws DaoException {
        int rows = 0;
		try {
			rows = sqlMapClient.update("tb_jpoa_provider.ibatorgenerated_updateByPrimaryKey", record);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
        return rows;
    }
    
    
    public ResultDto<Provider> findProviderInfoForPage(ProviderDTO providerDTO)
	        throws DaoException
	{

		ResultDto<Provider> result = new ResultDto<Provider>();
		try
		{
			List<Provider> configsList = sqlMapClient.queryForList(
			        "tb_jpoa_provider.find_provider_page", providerDTO);
			
			result.setResult(configsList != null ? configsList
			        : new ArrayList<Provider>());
			
			BeanUtils.copyProperties(providerDTO.getPagination(),
			        result.getPagination());
			result.getPagination()
			        .setTotal(
			                (Integer) sqlMapClient.queryForObject(
			                        "tb_jpoa_provider.find_provider_page_count",
			                        providerDTO));
		} catch (DaoException e)
		{
			LOGGER.error("findProviderInfoForPage exception", e);
			throw new DaoException("findProviderInfoForPage exception", e);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}
}